const app = require("../app/index");
const {
  NAME_IS_EXISTS,
  NAME_OR_PASSWORD_IS_REQUIRED,
  NAME_IS_NOT_EXISTS,
  PASSWORD_ERROR,
  UNAUTHORIZATION,
  UNPERMISSION,
} = require("../constant/index");

app.on("error", (error, ctx) => {
  let code = 0;
  let message = "";
  switch (error) {
    case NAME_OR_PASSWORD_IS_REQUIRED:
      code = -1001;
      message = "用户名或者密码不能为空";
      break;
    case NAME_IS_EXISTS:
      code = -1002;
      message = "用户名已被占用~";
      break;
    case NAME_IS_NOT_EXISTS:
      code = -1003;
      message = "用户名不存在~";
      break;
    case PASSWORD_ERROR:
      code = -1004;
      message = "密码错误~";
      break;
    case UNAUTHORIZATION:
      code = 403;
      message = "无登录权限，请返回重新登录~";
      break;
    case UNPERMISSION:
      code = -1005;
      message = "无操作权限";
      break;

    default:
      break;
  }

  ctx.body = { message, code };
});
